<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="display: flex;padding: 20px;">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id"
        ><template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="imagePath">
        <template slot-scope="scope">
          <a :href="'http://localhost:8088'+scope.row.imagePath" target="_blank">
          <img :src="'http://localhost:8088'+scope.row.imagePath" alt="" srcset="" width="60">
          </a>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="上传图片" prop="" ref="imagePaths">
           <el-upload
            class="upload-demo"
            :action="uploadFileUrl"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="1"
            :on-success="handleSuccessWay"
            :before-upload="beforeAvatarUploadBus"
            :on-exceed="handleExceed"
            :file-list="fileWay">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png/bmp/gif文件，且不超过15MB</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryList, add, deleteOther } from "@/api/getData.js"
export default {
  name: "Circuit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
      },
      // 表单参数
      form: {},
      // 表单校验
      uploadFileUrl:'/api/food/uploadFile',
      fileWay: [],
      rules: {
        name: [
          { required: true, message: "文件名不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        imagePath: [
          { required: true, message: "图片不能为空", trigger: "chang" }
        ],
      },
      tableHeight:0
    };
  },
  created() {
    this.getList();
  },
  mounted(){
    let that = this;
    this.$nextTick(() => {
      that.tableHeight = document.documentElement.clientHeight - 85 + "px";
      window.onresize = () => {
        return (() => {
          that.tableHeight = document.documentElement.clientHeight - 85 + "px";
        })();
      };
    });
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if(!this.queryParams.name){
        this.queryParams = {}
      }
      queryList(this.queryParams).then(response => {
        this.postList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        imagePaths: undefined,
        price: undefined,
      };
      this.fileWay = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增商品";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.postId != undefined) {
           
          } else {
            add(this.form).then(response => {
              this.$Message.success('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除该商品？').then(function() {
        return deleteOther({goodsId: row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleRemove(file, fileList) {
    },
    handlePreview(file) {
    },
    handleExceed(files, fileList) {
      this.$message.error(`只能上传一个文件！`);
    },
    beforeRemove(file, fileList) {
      this.fileWay = []
      this.form.imagePath = ""
    },
    handleSuccessWay(res, file) {
       this.form.imagePath = res
      let _this = this
       setTimeout(function () {
                _this.$refs.imagePaths.clearValidate();//消除校验,这里的setTimeOut不能去掉,去掉之后会不生效
        },30);
    },
    beforeAvatarUploadBus(file) {
      const isJPG =
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/bmp' ||
        file.type === 'image/gif' ||
        file.type === 'image/jpg'
      const isLt1M = file.size / 1024 / 1024 < 15
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 15MB!')
        return false
      }
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG、JPEG、PNG、BMP 格式!')
        return false
      }
      return true
    },
  }
  
};
</script>
